<?php
namespace app\controllers;
use app\models\Class1;
use app\models\Class2;
use app\models\DataRelationship;
use app\models\ReadContent;
use app\models\ReadOption;
use app\models\ReadQuestion;
use app\models\SpeakContent;
use app\models\SpeakPerson;
use app\models\WritePerson;
use app\models\WriteQuestion;
use \yii\web\Controller;
class CommunityController extends Controller
{
    public $layout = 'community';
    public function beforeAction($action)
    {
        $session = \Yii::$app->session;
        $session->open();
        if (!isset($session['user_id'])) {
            if(isset($_COOKIE['dianke_user_id']))
                $this->redirect('?r=site/dologin&islogin='.$_COOKIE['dianke_user_id']);
            else
                $this->redirect('?r=site/login');
        }
        return parent::beforeAction($action);
    }

    public function actionReading(){
        $session = \Yii::$app->session;
        $session->open();
        $request = \Yii::$app->request;
        $id=$request->get('id');
        if ($request->isPost)
        {
            $que=$request->post('que');
            $read_content = $session['read_content'];
            $read_question = $session['read_question'];
            $que_option=$session['que_option'];
            $read_question_num=count($read_question);
            if($que>$read_question_num) $que=1;
            $read_option=ReadOption::find()->where(['read_question_id'=>$read_question[$que-1]['id']])->all();
            if($session['reading_complete'])
            {
                $answer=$session['read_user_answer'];
                $t_time=$session['t_time'];
                $user_key=$session['user_key'];
                $result=['read_content' => $read_content,
                    'read_question' => $read_question,
                    'read_option' => $read_option,
                    'que' => $que+1,
                    'read_question_num' => $read_question_num,
                    'class2_name' => $session['class2_name'],
                    'answer'=>$answer,
                    'que_option'=>$que_option,
                    'user_key'=>$user_key,
                    'action'=>'reading',
                    't_time'=>$t_time,
                    'show_ans'=>true
                ];
            }
            else
            {
                $result=['read_content' => $read_content,
                    'read_question' => $read_question,
                    'read_option' => $read_option,
                    'que' => $que+1,
                    'read_question_num' => $read_question_num,
                    'class2_name' => $session['class2_name'],
                    'action'=>'reading',
                    'show_ans'=>false
                ];
            }
            switch ($read_question[$que-1]['type'])
            {
                case 1:
                    if(!$session['read_practice']||!$session['reading_complete'])
                        return $this->render('r1',$result);
                    else
                        return $this->render('r2', $result);
                    break;
                case 2:
                    return $this->render('r2', $result);
                    break;
                case 3:
                    return $this->render('r3', $result);
                    break;
                case 4:
                    return $this->render('r4', $result);
                    break;
            }
        }
        else
        {
            if(!isset($id)&&$session['reading_complete'])
            {
                $read_content = $session['read_content'];
                $read_question = $session['read_question'];
                $read_question_num= $session['read_question_num'];
                $answer=$session['read_user_answer'];
                $read_option=ReadOption::find()->where(['read_question_id'=>$read_question[0]['id']])->all();
                $que_option=$session['que_option'];
                $t_time=$session['t_time'];
                $user_key=$session['user_key'];
                $result=['read_content' => $read_content,
                    'read_question' => $read_question,
                    'read_option' => $read_option,
                    'que' => 2,
                    'read_question_num' => $read_question_num,
                    'class2_name' => $session['class2_name'],
                    'answer'=>$answer,
                    'que_option'=>$que_option,
                    'user_key'=>$user_key,
                    'action'=>'reading',
                    't_time'=>$t_time,
                    'show_ans'=>true
                ];
            }
            else
            {
                if(!isset($id))
                    return $this->redirect('?r=site/index');
                $session['reading_complete']=false;
                $read_content=ReadContent::findOne($id);
                $read_question=ReadQuestion::find()->where(['read_content_id'=>$read_content['id']])->
                orderBy('tnum')->all();
                $read_option=ReadOption::find()->where(['read_question_id'=>$read_question[0]['id']])->all();
                $session['read_content']=$read_content;
                $session['read_question']=$read_question;
                $class2=Class2::findOne($read_content['class2_id']);
                $session['class2_name']=$class2['name'];
                $result=['read_content' => $read_content,
                    'read_question' => $read_question,
                    'read_option' => $read_option,
                    'que' => 2,
                    'read_question_num' => count($read_question),
                    'class2_name' => $session['class2_name'],
                    'action'=>'reading',
                    'show_ans'=>false
                ];
                $where=[
                    'user_id'=>$session['user_id'],
                    'class1_id'=>$session['read_content']['class1_id'],
                    'class2_id'=>$session['read_content']['class2_id'],
                    'content_id'=>$session['read_content']['id']
                ];
                $data_relation=DataRelationship::find()->where($where)->one();
                if(isset($data_relation))
                {
                    $answer=unserialize($data_relation['answer']);
                    $user_key=unserialize($data_relation['user_key']);
                    $t_time=unserialize($data_relation['t_time']);
                    $read_practice=unserialize($data_relation['reading_practice']);
                    $que_option=unserialize($data_relation['que_option']);
                    if($answer!=null&&$user_key!=null&&$t_time!=null)
                    {
                        $result['answer']=$answer;
                        $result['show_ans']=true;
                        $result['user_key']=$user_key;
                        $result['t_time']=$t_time;
                        $session['reading_complete']=true;
                        $session['read_user_answer']=$answer;
                        $session['user_key']=$user_key;
                        $session['t_time']=$t_time;
                        $session['read_practice']=$read_practice;
                        $session['que_option']=$que_option;
                    }
                }
            }
            
            switch ($read_question[0]['type'])
            {
                case 1:
                    if(!$session['read_practice']||isset($id))
                        return $this->render('r1',$result);
                    else
                        return $this->render('r2', $result);
                    break;
                case 2:
                    return $this->render('r2', $result);
                    break;
                case 3:
                    return $this->render('r3', $result);
                    break;
                case 4:
                    return $this->render('r4', $result);
                    break;
            }
        }
    }
    public function actionSpeaking()
    {
        $request=\Yii::$app->request;
        $id=$request->get('id');
        $speak_content=SpeakContent::find()->where(['id'=>$id])->one();
        $session=\Yii::$app->session;
        $session->open();
        $class2_id=Class2::find()->where(['id'=>$speak_content['class2_id']])->one();
        $speak_person=SpeakPerson::find()->where(['uid'=>$session['user_id'],'speak_content_id'=>$id])->one();
        $result=[
            'speak_content'=>$speak_content,
            'class2_id'=>$class2_id,
            'speak_person'=>$speak_person,
            'speak_id'=>$id
        ];
        if($speak_content['type']==1){
            return $this->render('s1',$result);
        }
        if($speak_content['type']==2){
            return $this->render('s3',$result);
        }
        if($speak_content['type']==3){
            return $this->render('s2',$result);
        }
    }
    public function actionWriting(){
        $request=\Yii::$app->request;
        $id=$request->get('id');
        $session = \Yii::$app->session;
        $session->open();
        $write_content=WriteQuestion::find()->where(['id'=>$id])->one();
        $write_person=WritePerson::find()->where(['id'=>$session['write_person_id']])->one();
        $write_person_content1=WritePerson::find()->where(['uid' =>$session['user_id'],'type'=>2,'write_question_id'=>$id])->one();
        $write_person_content2=WritePerson::find()->where(['uid' =>$session['user_id'],'type'=>1,'write_question_id'=>$id])->one();
        if(isset($write_person_content1)||isset($write_person_content2)) {
            if ($write_content['class_name'] == '综合写作') {
                return $this->render('w1', ['write_content' => $write_content, 'write_person' => $write_person,'write_person_content1'=>$write_person_content1]);
            }
            if ($write_content['class_name'] == '独立写作') {
                return $this->render('w2', ['write_content' => $write_content, 'write_person' => $write_person,'write_person_content2'=>$write_person_content2]);
            }
        }
        else{
            if ($write_content['class_name'] == '综合写作') {
                return $this->render('w1', ['write_content' => $write_content, 'write_person' => $write_person,'write_person_content1'=>$write_person_content1]);
            }
            if ($write_content['class_name'] == '独立写作') {
                return $this->render('w2', ['write_content' => $write_content, 'write_person' => $write_person,'write_person_content2'=>$write_person_content2]);
            }
        }
    }
}